<?php
/**
 * User: zyj
 * Date: 2019/8/28
 */

namespace app\manage\controller;
use app\BaseController;
use think\facade\Cache;
use think\facade\Config;
use think\facade\Env;
use think\facade\Session;
use think\response\View;
class Login extends BaseController
{
    /**
     * @author zyj<535985755@qq.com>
     * Date: 2019/11/8
     */
    public function index(){
        $this->view->layout(false);
        return view("/login/index");
    }
    /**
     * 登录时 用户信息存入session 并且前端跳转指定url
     * @author zyj<535985755@qq.com>
     * Date: 2019/11/8
     */
    public function doLogin(){
        $data['username']= $this->request->post("username","");
        $data['userpass'] = $this->request->post("userpass","");
        // 输入数据效验
           $loginValidate= validate(\app\manage\validate\Login::class)->failException(false);
           if(!$loginValidate->check($data)){
               return json(["code"=>10000,"message"=>$loginValidate->getError()]);
           }
        // 用户信息验证
        $user = (new \app\manage\logic\Login())->checkLogin($data);
        if(!isset($user)){
            return json(["code"=>10001,"message"=>"用户不存在"]);
        }
        $userInfo = ($user->hidden(['password']))->toArray();
        if(!empty($userInfo)){
            Session::set('user', $userInfo);
            return json(["code"=>0,"message"=>'登录成功，正在进入系统...',"url"=>'/manage/Index/index']);
        }
        return json(["code"=>10002,"message"=>"未知错误"]);

    }
    public function index1()
    {
        echo "HOST:".Env::get("HOST");
        echo "app_host".Config::get("app.app_host");
        return View::fetch();
    }


}